/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * Description:
 * Author: huawei
 * Create: 2022-11-29
 */
#ifndef SVM_DEV_RES_MNG_H
#define SVM_DEV_RES_MNG_H

#include <linux/kref.h>
#include <linux/hashtable.h>
#include <linux/rwlock_types.h>
#include <linux/device.h>

#include "devmm_common.h"

#ifndef DEVMM_UT
#define DEVMM_MSG_INIT_SEND_WAITTIME 1000
#else
#define DEVMM_MSG_INIT_SEND_WAITTIME 0
#endif

#define DEVMM_HASH_LIST_NUM_SHIFT 10
#define DEVMM_HASH_LIST_NUM (1 << DEVMM_HASH_LIST_NUM_SHIFT)  /* 1024 */

struct devmm_dev_msg_client {
    struct device *dev;
    void *msg_chan;
};

struct devmm_ipc_mem_node_info {
    DECLARE_HASHTABLE(node_htable, DEVMM_HASH_LIST_NUM_SHIFT);
    rwlock_t rwlock;
};

struct devmm_task_dev_res_info {
    struct list_head head;
    struct rw_semaphore rw_sem;
};

struct devmm_share_phy_addr_agent_blk_mng {
    struct rw_semaphore rw_sem;
    struct rb_root rbtree;
};

struct devmm_dev_res_mng {
    struct svm_id_inst id_inst;

    struct kref ref;

    struct devmm_dev_msg_client dev_msg_client;
    struct devmm_ipc_mem_node_info ipc_mem_node_info;

    struct devmm_task_dev_res_info task_dev_res_info;
    struct devmm_share_phy_addr_agent_blk_mng share_agent_blk_mng;
};

int devmm_dev_res_mng_create(struct svm_id_inst *id_inst, struct device *dev);
void devmm_dev_res_mng_destroy(struct svm_id_inst *id_inst);
struct devmm_dev_res_mng *devmm_dev_res_mng_get(struct svm_id_inst *id_inst);
void devmm_dev_res_mng_put(struct devmm_dev_res_mng *res_mng);
void devmm_dev_res_mng_destroy_all(void);

void devmm_init_task_dev_res_info(struct devmm_task_dev_res_info *info);

#endif /* SVM_DEV_RES_MNG_H */
